package org.blogwu.learn.jvmlearn.class1;

/**
 * Created by Administrator on 2018/6/21.
 */
public class MyTest6 {


    public static void main(String[] args) {
        Singleton s1 = Singleton.getInstance();
        System.out.println("---------------------");
        System.out.println("counter1:" + Singleton.counter1);
        System.out.println("counter2:" + Singleton.counter2);
        System.out.println("counter2:" + s1.counter2);
        System.out.println("counter3:" + s1.counter3);

        System.out.println("---------------------");

        Singleton1 s2 = Singleton1.getInstance();
        System.out.println("n1:" + Singleton1.n1);
        System.out.println("n2:" + Singleton1.n2);
    }
}


class Singleton {

    //从上到下加载

    public static int counter1 = 1; //1

    public int counter3 = 1;

    {
        System.out.printf("代码块执行");
    }

    private static Singleton singleton = new Singleton();//2

    private Singleton() {//2.1
        System.out.println("---------------------");
        counter1++;
        counter2++;
        counter3++;
        System.out.println("1counter1:" + counter1);
        System.out.println("1counter2:" + counter2);


    }

    public static int counter2 = 2;

    public static Singleton getInstance() {
        return singleton;
    }

}


class Singleton1 {

    public static String n1;

    private static Singleton1 singleton1 = new Singleton1();

    private Singleton1() {//2.1
        n1 = "n1";
//        n2="n22";
        System.out.println("1n1:" + n1);
        System.out.println("2n2:" + n2);
    }

    public static String n2 = "n2";

    public static Singleton1 getInstance() {
        return singleton1;
    }

}